package bud.web.controller;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.support.RequestContextUtils;

import bud.vo.BoardBudDTO;
import bud.vo.BoardFormDTO;
import bud.web.boardbud.service.IBoardbudService;
import bud.web.boardbud.service.IBoardbudService.BoardbudResult;


@Controller
public class BoardbudController {
	@Inject
	IBoardbudService service;
	
	@RequestMapping("boardbudList")
	public String boardbudList(@ModelAttribute("formDTO") BoardFormDTO formDTO) {
		if(formDTO.getCurrentPage()==null) {
			formDTO.setCurrentPage(1);
		}
		formDTO.setScreen_size(20);
		formDTO.setBlock_size(7);
		formDTO.setTotalRecords(service.getBoardbudCount(formDTO));

		//페이징 처리를 위한 어트리뷰트를 스코프에 추가
		List<BoardBudDTO> boardList = service.getBoardbudList(formDTO);
		formDTO.setDataList(boardList);
		return "boardbud/boardbudList";
	}
	
	@RequestMapping("boardbudForm")
	public String boardbudForm() {
		return "boardbud/boardbudForm";
	}
	
	@RequestMapping("boardbudInsert")
	public String boardbudInsert(@ModelAttribute("boardbud")BoardBudDTO boardbud, 
			BindingResult error, Model model) {
		String goPage=null;
		if(error.hasErrors()) {
			goPage = "boardbud/boardbudForm";
		} else {
			
				}//if문 끝
			
			BoardbudResult result = service.createBoardbud(boardbud);
			if(result == BoardbudResult.OK) {
				goPage = "redirect:/boardbudList.do";
			} else {
				model.addAttribute("msg", "알 수 없는 오류가 발생, 잠시 후 다시 시도해주세요.");
				goPage = "boardbud/boardbudForm";
			}
		return goPage;
	}
	
	@RequestMapping("boardbudView")
	public String boardbudView(int bo_no,HttpServletRequest request,
			Model model) {
		BoardBudDTO boardbud = service.getBoardbud(bo_no);
		if(boardbud == null) {
//			throw new BoardException("게시글 번호에 해당하는 글이 없습니다. 확인 후 이용해주세요.");
		}
		
		Map<String, ?> flashMap = RequestContextUtils.getInputFlashMap(request);
		if(flashMap!=null) {
			model.addAttribute("msg",flashMap.get("msg"));
		}
		
		//댓글 페이징을 위한 어트리뷰트 추가
		BoardFormDTO<BoardBudDTO> form = new BoardFormDTO<BoardBudDTO>();
		form.setCurrentPage(1);
		form.setScreen_size(10);
		form.setBlock_size(5);
		form.setTotalRecords(service.getRepCount(boardbud.getBo_no()));
		form.setData(boardbud);
		model.addAttribute("formDTO", form);
		model.addAttribute("boardbud", boardbud);
		return  "boardbud/boardbudView";
	}
	
	@RequestMapping("boardbudEdit")
	public String boardbudEdit(@RequestParam("bo_no") int bo_no,
			Model model) {
		BoardBudDTO boardbud = service.getBoardbud(bo_no);
		if(boardbud == null) {
//			throw new BoardException("게시글 번호에 해당하는 글이 없습니다. 확인 후 이용해주세요.");
		}
		model.addAttribute("boardbud", boardbud);
		return "boardbud/boardbudEdit";
	}
}


